package com.sense.kukoo.biz_me.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo

class FeedbackViewModel:BaseViewModel() {

    private val TAG: String = javaClass.simpleName

    val mDescription : MutableLiveData<String> = MutableLiveData("")
    val mPhone : MutableLiveData<String> = MutableLiveData("")
    val mSubmitBtnEnabel:MutableLiveData<Boolean> = MutableLiveData(false)
    val mSubmitResult:MutableLiveData<Boolean> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {}

    fun checkSubmitData(){
        val des = mDescription.value ?: ""
        val phone = mPhone.value ?: ""
        var enable = false
        /*enable = phone.length == 11 && des.length > 0*/
        enable = des.length > 0

        //
        mSubmitBtnEnabel.postValue(enable)
    }

    fun onSubmit(){
        toView(ACTION_SHOW_DIALOG)
        request {
            val result = Repo.http.feedback(mDescription.value!!, mPhone.value!!)
            val data:Boolean = result.data == true

            if (result.success && data) {
                mSubmitResult.postValue(true)
            } else {
                Log.e(TAG, "result=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }
}